﻿using System;
using System.Collections.Generic;
using System.Data;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using DevelopTool.Model.DbModels;
using DevelopTool.My.Plugins.Bll;
using Sunny.UI;

namespace DevelopTool.Forms.DbForms.ComSqlForms
{
    /// <summary>
    /// COM_SQL数据查询窗体
    /// </summary>
    public partial class ComSqlForm : PluginsForm
    {
        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;


        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("ID"){ColumnText="编码",Width=0,ColumnType=ColumnType.DefaultFilter|ColumnType.RowMain}},
                { new DataGridColumn("NAME"){ColumnText="Sql",Width=150,ColumnType=ColumnType.DefaultFilter|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="ID"
                }}},
                { new DataGridColumn("MEMO"){ColumnText="备注",Width=0,ColumnType=ColumnType.DefaultFilter|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="ID",TextType="Text",ShowTextLength=20
                }}},
                { new DataGridColumn("OWNER"){ColumnText="实例",Width=100}},
                { new DataGridColumn("TABLE_NAME"){ColumnText="表",Width=0}},
                { new DataGridColumn("TYPE"){ColumnText="类型",Width=0}},
                { new DataGridColumn("MODUAL"){ColumnText="模块",Width=0}},
                { new DataGridColumn("ISVALID"){ColumnText="状态",Width=-1}},
              };

        public ComSqlForm()
        {
            InitializeComponent();

        }


        private void ReportQueryForm_Load(object sender,EventArgs e)
        {
          
        }


        private void LoadDate(DbInfo dbInfo)
        {            
            try
            {
                DataTable dataTable = new DataTable();

                LoadingHelper.ShowLoading("获取COM_SQL数据",() =>
                {                   
                   dataTable = new ComSqlBll(dbInfo).GetComSqlData();//查询Com_Sql表数据
                });

                if(dataTable.Rows.Count <= 0)
                {
                    this.ShowWarningDialog("此数据库中没有COM_SQL表");
                    return;
                }

                this.dataDisplayControl.BindData(dataTable,this.dataGridColumns,"TABLE_NAME ASC");
            }
            catch(Exception ex)
            {
                this.ShowErrorDialog(ex.Message);
            }
        }


        private void dataDisplayControl_DataReLoad(object sender,EventArgs e)
        {
            LoadDate(this.dbInfo);
        }


        private void dbinfoComboBox_DbInfoSelected(object sender,CommonControls.DbinfoEventArgs e)
        {
            if(e.IsConnect)
            {
                this.dbInfo = e.DbInfo;

                LoadDate(this.dbInfo);

            }
        }

    }
}
